<template>
  <div id="histogram" style="height: 270px"></div>
</template>

<script>
import * as echarts from "echarts";
import { GridComponent } from "echarts/components";
import { BarChart } from "echarts/charts";
import { CanvasRenderer } from "echarts/renderers";

echarts.use([GridComponent, BarChart, CanvasRenderer]);
export default {
  props: {
    dataValue: Array,
  },
  data() {
    return {
      hisTitle: [],
      hisValue: [],
    };
  },
  methods: {
    selectInfo() {
      for (var i = 0; i < this.dataValue.length; i++) {
        this.hisValue.push(this.dataValue[i].value);
        this.hisTitle.push(this.dataValue[i].name);
      }
      var chartDom = document.getElementById("histogram");
      var myChart = echarts.init(chartDom);

      var option;
      option = {
        title: {
          text: "柱状统计图",
          subtext: "Fake Data",
          left: "center",
        },
        color: {
          type: "linear",
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: "#13b9ff", 
            },
            {
              offset: 1,
              color: "#b51fff", 
            },
          ],
        },
        xAxis: {
          type: "category",
          data: this.hisTitle,
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: this.hisValue,
            type: "bar",
          },
        ],
        tooltip: {
          trigger: "item",
        },
      };
      option && myChart.setOption(option);
    },
  },
  mounted() {
    this.selectInfo();
  },
};
</script>